package com.lc.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lc.reggie.dao.SetmealMapper;
import com.lc.reggie.dto.SetmealDto;
import com.lc.reggie.entity.Setmeal;
import com.lc.reggie.entity.SetmealDish;
import com.lc.reggie.service.SetmealDishService;
import com.lc.reggie.service.SetmealService;
import com.lc.reggie.common.CustomException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService
{
	@Autowired
	private SetmealDishService setmealDishService;
	
	
	/**
	 * 新增套餐同时保存需要保存套餐和菜品的关联关系
	 * @param setmealDto
	 */
	@Override
	@Transactional
	public void saveWithDish(SetmealDto setmealDto)
	{
		//保存套餐的基本信息，操作setmeal，执行insert操作
		this.save(setmealDto);

		//保存套餐和菜品的关联信息，操作setmeal_dish,执行insert操作
		Long setmealId = setmealDto.getId();
		List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
		setmealDishes = setmealDishes.stream().map((item) -> {
			item.setSetmealId(setmealId);
			return item;
		}).collect(Collectors.toList());
		
		setmealDishService.saveBatch(setmealDishes);
	}
	
	/**
	 * 删除套餐，同时需要删除套餐和菜品的关联关系
	 * @param ids
	 */
	@Override
	@Transactional
	public void removeWithDish(List<Long> ids)
	{
		//select count(*) from setmeal where id in (1,2,3) and status = 1
		//查询套餐状态，确定是否可用删除
		LambdaQueryWrapper<Setmeal> lqw1 = new LambdaQueryWrapper<>();
		lqw1.in(Setmeal::getId, ids);
		lqw1.eq(Setmeal::getStatus, 1);
		int count = this.count(lqw1);
		
		if (count > 0)
		{
			//如果不能删除，抛出一个业务异常
			throw new CustomException("套餐正在售卖中，不能删除");
		}
		
		//如果可以删除，先删除套餐表中的数据---setmeal
		this.removeByIds(ids);
		
		//删除关系表中的数据----setmeal_dish
		LambdaQueryWrapper<SetmealDish> lqw2 = new LambdaQueryWrapper<>();
		lqw2.in(SetmealDish::getSetmealId, ids);
		setmealDishService.remove(lqw2);
	}
	
	/**
	 * 回显套餐信息包含已经选择的菜品
	 * @param id
	 * @return
	 */
	@Override
	public SetmealDto getByIdWithDish(Long id)
	{
		SetmealDto setmealDto = new SetmealDto();
		Setmeal setmeal = this.getById(id);
		BeanUtils.copyProperties(setmeal, setmealDto);
		
		LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
		lqw.eq(SetmealDish::getSetmealId, id);
		List<SetmealDish> list = setmealDishService.list(lqw);
		setmealDto.setSetmealDishes(list);
		
		return setmealDto;
	}
	
	/**
	 * 修改套餐信息包含已经选择的菜品关系
	 * @param setmealDto
	 */
	@Override
	@Transactional
	public void updateWithDish(SetmealDto setmealDto)
	{
		//修改套餐表信息--setmeal
		this.updateById(setmealDto);
		
		//修改套餐菜品表信息--setmeal_dish
		Long setmealId = setmealDto.getId();
		List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
		//1.先删除原先的关系
		LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
		lqw.eq(SetmealDish::getSetmealId, setmealId);
		setmealDishService.remove(lqw);
		
		//2.再新增修改后的关系
		setmealDishes = setmealDishes.stream().map((item) -> {
			item.setSetmealId(setmealId);
			return item;
		}).collect(Collectors.toList());
		
		setmealDishService.saveBatch(setmealDishes);
	}
	
	@Override
	@Transactional
	public void stopOrStartSell(Integer status, List<Long> ids)
	{
		List<Setmeal> setmeals = this.listByIds(ids);
//		//删除套餐表里面的信息
//		this.removeByIds(ids);
//		//新增套餐表里面的信息,设置好停售还是起售状态
//		setmeals = setmeals.stream().map((item) -> {
//			item.setStatus(status);
//			return item;
//		}).collect(Collectors.toList());
//		this.saveBatch(setmeals);
		for (Setmeal setmeal : setmeals)
		{
			setmeal.setStatus(status);
			this.updateById(setmeal);
		}
	}
}
